package com.xinchao.service.impl;

import com.xinchao.constant.AdvertisementConstant;
import com.xinchao.constant.BusinessConstant;
import com.xinchao.dao.AdvertisementMonitorMapper;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.dto.*;
import com.xinchao.model.vo.OverviewAdvertisementVO;
import com.xinchao.service.AdvertisementMonitorService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author dxy
 * @date 2018/9/29 16:39
 */
@Service(value = "advertisementMonitorService")
public class AdvertisementMonitorServiceImpl implements AdvertisementMonitorService {
	@Autowired
	private AdvertisementMonitorMapper advertisementMonitorMapper;

	/**
	 * 通过广告编码获取广告曝光次数的总和
	 *
	 * @param advertisementCode 广告编码
	 * @return Long（广告的监播数量）
	 * @throws ServiceException
	 */
	@Override
	public Long sumWatchNumberByAdvertisementCode(String advertisementCode) throws ServiceException {
		if (StringUtils.isBlank(advertisementCode)) {
			throw new ServiceException("输入参数不能为空");
		}
		return advertisementMonitorMapper.sumWatchNumberByAdvertisementCode(advertisementCode);
	}

	/**
	 * 通过广告名称统计曝光次数
	 *
	 * @param advertisementName 广告名称
	 * @return Double
	 */
	@Override
	public Long sumExposures(String advertisementName, Long startTime, Long endTime) {
		return advertisementMonitorMapper.sumExposures(advertisementName, startTime, endTime);
	}

	/**
	 * 通过广告名称统计总时长
	 *
	 * @param advertisementName 广告名称
	 * @return Double
	 */
	@Override
	public PlayDurationWatchSumDTO getPlayDurationWatchDTO(String advertisementName, Long startTime, Long endTime) {
		return advertisementMonitorMapper.getPlayDurationWatchDTO(advertisementName, startTime, endTime);
	}

	/**
	 * 通过广告名称获取曝光次数、触达人次
	 *
	 * @param advertisementName 广告名称
	 * @return TouchWatchSumDTO
	 */
	@Override
	public TouchWatchSumDTO getTouchWatchSumDTO(String advertisementName, Long startTime, Long endTime) {
		return advertisementMonitorMapper.getTouchWatchSumDTO(advertisementName, startTime, endTime);
	}

	/**
	 * 获取广告的终端曝光次数列表
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         结束时间
	 * @param endTime           开始时间
	 * @param size              条数
	 * @return List<DeviceExposuresSumDTO>
	 */
	@Override
	public List<DeviceExposuresSumDTO> listDeviceExposuresSumDTO(String advertisementName, Long startTime, Long endTime, Integer size) {
		if (size == null) {
			size = BusinessConstant.TOP_SIZE;
		}
		return advertisementMonitorMapper.listDeviceExposuresSumDTO(advertisementName, startTime, endTime, size);
	}

	/**
	 * 获取广告的终端观看人次列表
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         结束时间
	 * @param endTime           开始时间
	 * @param size              条数
	 * @return List<DeviceWatchSumDTO>
	 */
	@Override
	public List<DeviceWatchSumDTO> listDeviceWatchSumDTO(String advertisementName, Long startTime, Long endTime, Integer size) {
		if (size == null) {
			size = BusinessConstant.TOP_SIZE;
		}
		return advertisementMonitorMapper.listDeviceWatchSumDTO(advertisementName, startTime, endTime, size);
	}

	/**
	 * 获取广告的终端曝光次数列表
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         结束时间
	 * @param endTime           开始时间
	 * @return List<DevicePlayDurationWatchSumDTO>
	 */
	@Override
	public List<DevicePlayDurationWatchSumDTO> listDevicePlayDurationWatchSumDTO(String advertisementName, Long startTime, Long endTime) {
		return advertisementMonitorMapper.listDevicePlayDurationWatchSumDTO(advertisementName, startTime, endTime);
	}

	/**
	 * 通过广告名称统计观看人次
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         开始时间
	 * @param endTime           结束时间
	 * @return Double
	 */
	@Override
	public Long sumWatch(String advertisementName, Long startTime, Long endTime) {
		return advertisementMonitorMapper.sumWatch(advertisementName, startTime, endTime);
	}

	/**
	 * 通过时间获取广告监播总数
	 *
	 * @param advertisementName 广告名称
	 * @param startTime         开始时间
	 * @param endTime           结束时间
	 * @param deviceNumber      机器码
	 * @return IndexAdvertisementMonitorSumVO
	 * @throws ServiceException
	 */
	@Override
	public AdvertisementMonitorSumDTO getAdvertisementMonitorSumByTime(String advertisementName, Long startTime, Long endTime, String deviceNumber) throws ServiceException {
		return advertisementMonitorMapper.getAdvertisementMonitorSumByTime(advertisementName, startTime, endTime, deviceNumber);
	}

	/**
	 * 通过广告名称获取终端列表
	 *
	 * @param advertisementName 广告名称
	 * @return List<String>
	 * @throws ServiceException
	 */
	@Override
	public List<String> listDeviceSumByAdvertisementName(String advertisementName) throws ServiceException {
		if (StringUtils.isBlank(advertisementName)) {
			throw new ServiceException("输入参数不能为空");
		}
		return advertisementMonitorMapper.listDeviceSumByAdvertisementName(advertisementName);
	}

	/**
	 * 通过广告名称获取总览广告监播数据
	 * @param advertisementName 广告名称
	 * @return OverviewAdvertisementVO
	 * @throws ServiceException
	 */
	@Override
	public OverviewAdvertisementVO getOverviewAdvertisement(String advertisementName) throws ServiceException {
		if (StringUtils.isBlank(advertisementName)) {
			throw new ServiceException("广告名称不能为空");
		}
		AdvertisementMonitorSumDTO dto = getAdvertisementMonitorSumByTime(advertisementName, null, null, null);
		OverviewAdvertisementVO vo = new OverviewAdvertisementVO();
		if (dto == null) {
			//曝光次数
			vo.setExposuresSum(0L);
			//触达人次
			vo.setTouchSum(0L);
			//观看人次
			vo.setWatchSum(0L);
			//平均观看时长=总时长/观看人次
			vo.setAvgWatchDuration("0");
		} else {
			//曝光次数
			vo.setExposuresSum(dto.getExposuresSum());
			//触达人次
			vo.setTouchSum(dto.getTouchSum());
			//观看人次
			Long watchSum = dto.getWatchSum();
			vo.setWatchSum(watchSum);
			//总时长
			Long playDurationSum = dto.getPlayDurationSum();
			//平均观看时长=总时长/观看人次
			if (watchSum == null || watchSum == 0) {
				vo.setAvgWatchDuration("0");
			} else {
				if (playDurationSum == null || playDurationSum == 0) {
					vo.setAvgWatchDuration("0");
				} else {
					double watchAvg = playDurationSum / 1000 / watchSum;
					String watchAvgStr = String.format("%.2f", watchAvg);
					vo.setAvgWatchDuration(watchAvgStr);
				}
			}
		}
		//观看次数
		Long sumWatch = sumWatch(advertisementName, null, null);
		//消费(广告A的消费金额=广告A的观看人次*￥0.01)
		if (sumWatch == null) {
			vo.setConsumedCharge(0);
		} else {
			BigDecimal consumedCharge = new BigDecimal(sumWatch).multiply(AdvertisementConstant.PER_ADVERTISEMENT_CHARGE);
			//精确到两位数
			double value = consumedCharge.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			vo.setConsumedCharge(value);
		}
		//点位数
		List<String> deviceNumberList = listDeviceSumByAdvertisementName(advertisementName);
		if (CollectionUtils.isNotEmpty(deviceNumberList)) {
			vo.setPointLocationSum(deviceNumberList.size());
		} else {
			vo.setPointLocationSum(0);
		}
		return vo;
	}

	/**
	 * 统计观看人次
	 * @param advertisementCodeList 广告编码列表
	 * @return Long
	 * @throws ServiceException
	 */
	@Override
	public Long sumWatchNumber(List<String> advertisementCodeList) throws ServiceException {
		if (CollectionUtils.isEmpty(advertisementCodeList)) {
			throw new ServiceException("输入参数不能为空");
		}
		return advertisementMonitorMapper.sumWatchNumber(advertisementCodeList);
	}

}
